package gases.as3.resolve
{
	import flash.utils.Dictionary;
	
	import gases.as3.util.ComponentName;
	
	
	public class CoreResolve
	{
		private static var _instance:CoreResolve;
		
		private var _baseXML:XML;
		private var _baseDict:Dictionary;
		private var _templateDict:Dictionary;
		private var _templateParamsDict:Dictionary;//模板对外属性字典
		
		public function CoreResolve()
		{
			_baseDict     =new Dictionary();
			_templateDict =new Dictionary();
			_templateParamsDict =new Dictionary();
		}
		
		/**
		 *获得单例 
		 * @return CoreResolve
		 * 
		 */		
		public static function get instance():CoreResolve
		{
			if(!_instance)
			{
				_instance = new CoreResolve();
			}
			return _instance;
		}
		
		public function setBaseXML(xml:XML):void
		{
			_baseXML = xml;
			if(!_baseXML)
			{
				return;
			}
			addToDict();
		}
		
		public function setTemplate(xml:XML):void
		{
			addTemplate(xml);
		}
		/**
		 *增加模块的配置 
		 * @param xml
		 * 
		 */		
		private function addTemplate(xml:XML):void
		{
			var xmlList:XMLList = xml.children();
			for each(var newXML:XML in xmlList)
			{
				var id:String = newXML.@id.toString();
				var paramXMLList:XMLList =(newXML.copy()).elements("param");
				_templateParamsDict[id] = paramXMLList;
				for(var i:int=0;i<newXML.children().length();i++)
				{
					var childXML:XML = newXML.children()[i];
					if(childXML.localName() == "param")
					{
						delete newXML.children()[i];
						i--;
					}
				}
				
				_templateDict[id] = newXML;
			}
		}
		
		/**
		 * 增加简单组件的基本配置
		 * 
		 */		
		private function addToDict():void
		{
			var xmlList:XMLList = _baseXML.children();
			
			for(var i:int=0;i<xmlList.length();i++)
			{
				var xml:XML = xmlList[i];
				_baseDict[xml.localName().toString()] = xml;
			}
		}
		
		/**
		 * 解析xml
		 * @param xml
		 * @return 
		 * 
		 */		
		public function analyzeXML(xml:XML):XML
		{
			_targetXML = xml;
			var xmlList:XMLList = _targetXML.children();
			
			for(var num:int=0;num<xmlList.length();num++)
			{
				var newsXML:XML = xmlList[num];
				var templateName:String = newsXML.@template.toString();
				var newsXMLList:XMLList = newsXML.children();
				var attributesList:XMLList = newsXML.attributes();
				
				if(templateName.length >0)
				{
					var templateXMLOld:XML = _templateDict[templateName] as XML;
					try
					{
						var templateXML:XML = templateXMLOld.copy();
					}
					catch(e:Error)
					{
					}
					if(templateXML)
					{
						var componentName:String = templateXML.localName();
						var componentXML:XML = _baseDict[componentName] as XML;
						templateXML = retXML(componentXML,templateXML);
						for(var j:int=0;j<attributesList.length();j++)
						{
							var nm:String = attributesList[j].name();
						   if(templateXML.@[nm].toString().length != 0) 
							{
								templateXML.@[nm] = attributesList[j];
							}
							else
							{
								var arr:Array = getTemplateParamAttribute(templateName,nm);
								if(arr)
								{
									var id:String = arr[0];
									var value:String = arr[1];
									var templateXMLlist:XMLList = templateXML.children();
									for(var k:int=0;k<templateXMLlist.length();k++)
									{
										var child:XML = templateXMLlist[k];
										var childId:String = child.@id.toString();
										if(childId == id)
										{
											templateXMLlist[k].@[value] = attributesList[j];
										}
									}
								}
							}
						}
						
						for(var len:int=0;len<newsXMLList.length();len++)
						{
							var oldXML:XML = newsXMLList[len];
							var oldXMLId:String = oldXML.@id.toString();
							for(var len1:int = 0;len1<templateXML.children().length();len1++)
							{
								var temXML:XML = templateXML.children()[len1];
								if(oldXMLId == temXML.@id.toString())
								{
									templateXML.children()[len1] = retXML(templateXML.children()[len1],oldXML);
								}
							}
						}
						xmlList[num] = templateXML;
						newsXMLList = (xmlList[num] as XML).children();
					}
				}
				else
				{
					var unTemplateXMLName:String = newsXML.localName().toString();
					var unTemplateBaseXML:XML = (_baseDict[unTemplateXMLName] as XML).copy();
					newsXML = retXML(unTemplateBaseXML,newsXML);
					newsXMLList = newsXML.children();
				}
				for(var i:int = 0;i<newsXMLList.length();i++)
				{
					var localName:String = newsXMLList[i].localName();
					var baseXML:XML = (_baseDict[localName] as XML).copy();
					newsXMLList[i] = retXML(baseXML,newsXMLList[i]);
				}
			}
			return _targetXML;
		}
		
		/**
		 *获取公有属性 
		 * @param id
		 * @param name
		 * 
		 */		
		private function getTemplateParamAttribute(id:String,name:String):Array
		{
			var retArr:Array;
			var xmlList:XMLList = _templateParamsDict[id] as XMLList;
			if(xmlList)
			{
				for(var i:int=0;i<xmlList.length();i++)
				{
					var xml:XML = xmlList[i];
					if(xml.@id.toString() == name)
					{
						var str:String = xml.text().toString();
						var arr:Array = str.split(".");
						retArr = arr;
					}
				}
			}
			return retArr;
		}
		/**
		 *返回xml 
		 * @param xml
		 * @return 
		 * 
		 */		
		public function retXML(baseXML:XML,xml:XML):XML
		{
			var attributesList:XMLList = baseXML.attributes();
			for(var i:int=0;i<attributesList.length();i++)
			{
				var nm:String = attributesList[i].name();
				var value:Object = retValue(xml,nm);
				if(value)
				{
					xml.@[nm] = value;
				}
				else
				{
					xml.@[nm] = attributesList[i];
				}
			}
			return xml;
		}
		
		/**
		 *返回值 
		 * @param xml
		 * @param str
		 * @return 
		 * 
		 */		
		private function retValue(xml:XML,str:String):Object
		{
			var obj:Object;
			var attributesList:XMLList = xml.attributes();
			for(var i:int=0;i<attributesList.length();i++)
			{
				var nm:String = attributesList[i].name();
				var value:Object = attributesList[i];
				if(str == nm)
				{
					obj = value;
				}
			}
			return obj;
		}
		
		
		private var _targetXML:XML;
	}
}